package com.wframe.project.former.web.integral;

import com.alibaba.fastjson.JSON;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.pageform.DataPageForm;
import com.frame.pageform.annotation.Login;
import com.sun.org.apache.bcel.internal.generic.IF_ICMPLE;
import com.wframe.project.former.dataservices.integral.IntegralRuleService;
import com.wframe.project.former.dataservices.integral.IntegralService;
import com.wframe.project.former.dataservices.user.UserService;
import com.wframe.project.jnd.entity.IntegralRulesVo;
import jodd.util.StringUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/19.
 */
@Login
public class IntegralPage extends DataPageForm {
    private JdataBean bean;
    private JdataBean company;
    private UserService userService = UserService.getIntance();
    private IntegralService integralService = IntegralService.getIntance();
    private IntegralRuleService integralRuleService = IntegralRuleService.getIntance();
    @Override
    public String execute() throws Exception {

        setPagination(integralService.queryIntegral(bean,getPageSize_(),getPageNum()));


        return "/webadmin/integral/integralList.jsp";
    }

    /**
     * 收支详情
     * @return
     * @throws Exception
     */
    @Override
    public String show() throws Exception {

        setPagination(integralService.getCompanyedByUid(bean.getString("id"),getPageSize_(),getPageNum()));
        return "/webadmin/integral/integralDetails.jsp";
    }

    /**
     * 积分修改
     * @return
     * @throws Exception
     */
    public  String integralModify() throws Exception{
        bean = integralService.findIntegralId(bean.getString("id"));
        return "/webadmin/integral/integralModify.jsp";
    }

    /**
     * 回填积分规则数据
     * @return
     * @throws Exception
     */
    public  String integralSetting() throws Exception{
        bean=integralRuleService.queryIntegralRule();
       String dayintegral = bean.getString("dayintegral");
        List<IntegralRulesVo> jsonarr = JSON.parseArray(dayintegral,IntegralRulesVo.class);
       List list = new ArrayList();
        for (IntegralRulesVo irv :jsonarr){
            list.add(irv);
        }
       getRequest().setAttribute("rulelist",list);
        return "/webadmin/integral/integralSetting.jsp";
    }

    /**
     * 修改积分保存
     */
 /*   public void alterIntegral() throws Exception{
        if (StringUtil.isNotBlank(bean.getString("alterintegral"))){
            JdataBean jdata = new JdataBean();
            jdata.put("integral",bean.getString("alterintegral"));
            Map key = new HashMap();
            key.put("ID",bean.getString("ID"));
            userService.update(jdata.getDataMap(),key);
            getJsonObject().getJsonPage().closeWin("ok");
        }


    }*/

    @Override
    public String toDel() throws Exception {
        return null;
    }


    @Override
    public boolean toUpdate() throws Exception {
        JdataBean basedata = new JdataBean();
        try{
            if (StringUtil.isNotBlank(bean.getString("scale"))){
                JdataBean jdata = new JdataBean();
                jdata.put("val",bean.getString("scale"));
                Map key = new HashMap();
                key.put("code","integral_scale");
                integralRuleService.update(jdata.getDataMap(),key);
            }
            if (StringUtil.isNotBlank(bean.getString("register"))){
                JdataBean jdata = new JdataBean();
                jdata.put("val",bean.getString("register"));
                Map key = new HashMap();
                key.put("code","integral_register");
                integralRuleService.update(jdata.getDataMap(),key);
            }
            if (StringUtil.isNotBlank(bean.getString("today"))){
                JdataBean jdata = new JdataBean();
                jdata.put("val",bean.getString("today"));
                Map key = new HashMap();
                key.put("code","integral_today");
                integralRuleService.update(jdata.getDataMap(),key);
            }
            if (StringUtil.isNotBlank(bean.getString("integral"))){
                JdataBean jdata = new JdataBean();
                jdata.put("val",bean.getString("integral"));
                Map key = new HashMap();
                key.put("code","integral_rule");
                integralRuleService.update(jdata.getDataMap(),key);
            }
            /**
             * 积分修改后保存
             */
            if (StringUtil.isNotBlank(bean.getString("alterintegral"))){
                JdataBean jdata = new JdataBean();
                jdata.put("integral",bean.getString("alterintegral"));
                Map key = new HashMap();
                key.put("ID",bean.getString("ID"));
                userService.update(jdata.getDataMap(),key);
                getJsonObject().getJsonPage().closeWin("ok");
                getJsonObject().getJsonPage().showOkMsg("修改成功");
                getJsonObject().put("state", "ok");
            }
        }catch (Exception e) {
            Logger.getLogger(this.getClass()).error("修改信息错误：", e);
            getJsonObject().put("state", e.getMessage());
        }
        return false;
    }
    public JdataBean getBean() {
        return bean;
    }
    public void setBean(JdataBean bean) {
        this.bean = bean;
    }
}
